package org.qiaice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import lombok.RequiredArgsConstructor;
import org.qiaice.entity.Dish;
import org.qiaice.entity.DishFlavor;
import org.qiaice.entity.dto.DishDTO;
import org.qiaice.entity.dto.DishPageQueryDTO;
import org.qiaice.entity.vo.DishVO;
import org.qiaice.exception.BaseException;
import org.qiaice.mapper.DishMapper;
import org.qiaice.result.PageResult;
import org.qiaice.service.DishFlavorService;
import org.qiaice.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    private final DishFlavorService dishFlavorService;

    @Override
    public DishVO queryById(Long id) {
        DishVO dishVO = baseMapper.queryById(id);
        List<DishFlavor> dishFlavors = Db.lambdaQuery(DishFlavor.class).eq(DishFlavor::getDishId, id).list();
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    public PageResult<DishVO> pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        IPage<DishVO> dishVOIPage = baseMapper.pageQuery(dishPageQueryDTO.getCategoryId(),
                dishPageQueryDTO.getName(), dishPageQueryDTO.getStatus(),
                Page.of(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize()));
        return new PageResult<>(dishVOIPage.getTotal(), dishVOIPage.getRecords());
    }

    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        ids = lambdaQuery().in(Dish::getId, ids)
                .eq(Dish::getStatus, 0).list()
                .stream().map(Dish::getId).toList();
        if (ids.isEmpty()) {
            throw new BaseException("在删除菜品之前请先停售该菜品");
        }
        removeByIds(ids);
        dishFlavorService.remove(new LambdaQueryWrapper<DishFlavor>().in(DishFlavor::getDishId, ids));
    }

    @Override
    public void saveDish(DishDTO dishDTO) {
        Dish dish = Dish.builder().build();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setStatus(1);
        save(dish);
        Long dishId = dish.getId();
        dishDTO.getFlavors()
                .forEach(item -> item.setDishId(dishId));
        dishFlavorService.saveBatch(dishDTO.getFlavors());
    }

    @Override
    public void change(DishDTO dishDTO) {
        Dish dish = Dish.builder().build();
        BeanUtils.copyProperties(dishDTO, dish);
        updateById(dish);
        Long dishId = dish.getId();
        dishFlavorService.remove(new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId, dishId));
        dishDTO.getFlavors().forEach(item -> item.setDishId(dishId));
        dishFlavorService.saveBatch(dishDTO.getFlavors());
    }
}
